<table style="border: solid 1px;">
<?php
	function installfiles($title, $files) {
		echo '<tr><td>' . $title . ':&nbsp;&nbsp;&nbsp;</td>';
		$errors = array();
		$i = 0;
		for ($i = 0; $i < sizeof($files); $i++) {
			if (@copy($files[$i][0], $files[$i][1]) === FALSE) {
				$errors[] = array($files[$i][0], $files[$i][1]);
			}
		}
		if (sizeof($errors) < 1) {
			echo '<td><font color="#009900">OK</font> (' . $i . ' copied)</td></tr>';
			return TRUE;
		} else {
			echo '<td><font color="#990000">ERROR COPYING:</font><br/>';
			for ($i = 0; $i < sizeof($errors); $i++) {
				echo $errors[$i][0] . ' to ' . $errors[$i][1] . '<br/>';
			}
			echo '</td></tr>';
			return FALSE;
		}
	}

	function installmkdir($path) {
		echo '<tr><td>... ' . substr($path, -55, 55) . ':&nbsp;&nbsp;&nbsp;</td>';
		if (file_exists($path)) {
			echo '<td><font color="#009900">OK</font> (already exists)</td></tr>';
			return TRUE;
		} else if (@mkdir($path) === TRUE) {
			echo '<td><font color="#009900">OK</font> (created)</td></tr>';
			return TRUE;
		} else {
			echo '<td><font color="#990000">ERROR</font> (Check permissions)</td></tr>';
			return FALSE;
		}
	}

	// write dbconfig.inc.php using values from previous page
		
	function makeconfig($dbhost, $dbuser, $dbpass, $dbname, $tpfx){
		$cfile = 'shoppingCart/dbconfig.inc.php';
		$wrkcpy = fopen($cfile, 'w') or die("Can't open dbconfig...is it writeable?");
		$str = '<?php' . "\n" . '$dbhost="' . $dbhost . "\";\n" . '$dbname="' . $dbname . "\";\n" . '$dbuser="' . $dbuser . "\";\n" . '$dbpass="' . $dbpass . "\";\n" . '$tbl_pfx="' . $tpfx . "\";\n" . '?>';
		fwrite($wrkcpy, $str);
		fclose($wrkcpy);
	}

	installmkdir($basepath . $snippath . "shoppingCart");
	installmkdir($basepath . $snippath . "shoppingCart/includes");
	installmkdir($basepath . $mngrpath . "js");
	installmkdir($basepath . $mngrpath . "actions/ajaxprocs");
	installmkdir($basepath . $mngrpath . "actions/includes");

	installmkdir($basepath . $mngrpath . "js/modal_window");
	installmkdir($basepath . $mngrpath . "js/modal_window/images");
	installmkdir($basepath . $mngrpath . "js/modal_window/javascripts");
	installmkdir($basepath . $mngrpath . "js/modal_window/stylesheets");
	installmkdir($basepath . "assets/templates/kiwee");
	installmkdir($basepath . "assets/templates/kiwee/images");
	installmkdir($basepath . "assets/templates/kiwee/css");
	installmkdir($basepath . "assets/templates/kiwee/scripts");
	
	// Setup some variables to make changing file locations easier for us.
	$kw_base = "ecommerceInstaller/";
	$kw_snippets   = $kw_base . "kiwee/assets/snippets/";
	$kw_manager    = $kw_base . "kiwee/manager/";
	$kw_actions    = $kw_base . "kiwee/manager/actions/";
	$kw_templates  = $kw_base . "kiwee/templates/";
	
	
	// Install the snippets
	$files = array();
	$files[] = array($basepath . $kw_snippets . "shoppingCart/md5.js", $basepath . $snippath . "shoppingCart/md5.js");
	$files[] = array($basepath . $kw_snippets ."shoppingCart/cartmanager.js", $basepath . $snippath . "shoppingCart/cartmanager.js");
	$files[] = array($basepath . $kw_snippets . "shoppingCart/cartmanager.php", $basepath . $snippath . "shoppingCart/cartmanager.php");	
	$files[] = array($basepath . $kw_snippets . "shoppingCart/kiwee.class.php", $basepath . $snippath . "shoppingCart/kiwee.class.php");
	$files[] = array($basepath . $kw_snippets . "shoppingCart/db.class.php", $basepath . $snippath . "shoppingCart/db.class.php");
	$files[] = array($basepath . $kw_snippets . "shoppingCart/dbconfig.inc.php", $basepath . $snippath . "shoppingCart/dbconfig.inc.php");
	makeconfig($database_server, $database_user, $database_password, $dbase, $table_prefix);
	$files[] = array($basepath . $kw_snippets . "shoppingCart/checkout_buttons.gif", $basepath . $snippath . "shoppingCart/checkout_buttons.gif");
	$files[] = array($basepath . $kw_snippets . "shoppingCart/google_button.gif", $basepath . $snippath . "shoppingCart/google_button.gif");
	$files[] = array($basepath . $kw_snippets . "shoppingCart/paypal_button.gif", $basepath . $snippath . "shoppingCart/paypal_button.gif");
	$files[] = array($basepath . $kw_snippets . "shoppingCart/USD.xml", $basepath . $snippath . "shoppingCart/USD.xml");
	
	installfiles('Backend PHP code', $files);
	unset($files);



	// Install the Manager scripts
	$files = array();
	$files[] = array($basepath . $kw_manager . "js/prototype.js", $basepath . $mngrpath . "js/prototype.js");
	$files[] = array($basepath . $kw_manager . "js/modal_window/images/pdf.gif" , $basepath . $mngrpath . "js/modal_window/images/pdf.gif");
	$files[] = array($basepath . $kw_manager . "js/modal_window/images/print.gif" , $basepath . $mngrpath . "js/modal_window/images/print.gif");
	$files[] = array($basepath . $kw_manager . "js/modal_window/images/close.gif" , $basepath . $mngrpath . "js/modal_window/images/close.gif");
	$files[] = array($basepath . $kw_manager . "js/modal_window/images/overlay.png" , $basepath . $mngrpath . "js/modal_window/images/overlay.png");
	$files[] = array($basepath . $kw_manager . "js/modal_window/javascripts/modal.js" , $basepath . $mngrpath . "js/modal_window/javascripts/modal.js");
	$files[] = array($basepath . $kw_manager . "js/modal_window/javascripts/prototype.js", $basepath . $mngrpath . "js/modal_window/javascripts/prototype.js");
	$files[] = array($basepath . $kw_manager . "js/modal_window/stylesheets/modal.css" , $basepath . $mngrpath . "js/modal_window/stylesheets/modal.css");
	
	installfiles('Modal window files', $files);
	unset($files);

//	installmkdir($basepath . $mngrpath . "actions/includes/lang");
//	$files = array();
//	installfiles('Administration interface lang file', $files);
//	$files[] = array($basepath . "ecommerceInstaller/adminpages/lang/english.inc.php", $basepath . $mngrpath . "actions/includes/lang/english.inc.php");
//	unset($files);

	// Copy Admin Pages
	$files = array();
	$files[] = array($basepath . $kw_actions . "ajax.php", $basepath . $mngrpath . "actions/ajax.php");
	$files[] = array($basepath . $kw_actions . "ajaxprocs/drl.php", $basepath . $mngrpath . "actions/ajaxprocs/drl.php");
	$files[] = array($basepath . $kw_actions . "ajaxprocs/tax.php", $basepath . $mngrpath . "actions/ajaxprocs/tax.php");
	$files[] = array($basepath . $kw_actions . "ajaxprocs/lists.php", $basepath . $mngrpath . "actions/ajaxprocs/lists.php");
	$files[] = array($basepath . $kw_actions . "ajaxprocs/options.php", $basepath . $mngrpath . "actions/ajaxprocs/options.php");
	$files[] = array($basepath . $kw_actions . "ajaxprocs/products.php", $basepath . $mngrpath . "actions/ajaxprocs/products.php");
	$files[] = array($basepath . $kw_actions . "ajaxprocs/orders.php", $basepath . $mngrpath . "actions/ajaxprocs/orders.php");
	$files[] = array($basepath . $kw_actions . "ajaxprocs/customers.php", $basepath . $mngrpath . "actions/ajaxprocs/customers.php");
	$files[] = array($basepath . $kw_actions . "overview_kiwee.static.php", $basepath . $mngrpath . "actions/overview_kiwee.static.php");
	$files[] = array($basepath . $kw_actions . "includes/Kiwee.gif", $basepath . $mngrpath . "actions/includes/Kiwee.gif");
	$files[] = array($basepath . $kw_actions . "includes/Kiwee.gif", $basepath . "assets/images/Kiwee.gif");
	$files[] = array($basepath . $kw_actions . "includes/kiwee.css", $basepath . $mngrpath . "actions/includes/kiwee.css");
	$files[] = array($basepath . $kw_actions . "includes/Kiwee-small.gif", $basepath . $mngrpath . "actions/includes/Kiwee-small.gif");
	$files[] = array($basepath . $kw_actions . "ecommerce_config.dynamic.php", $basepath . $mngrpath . "actions/ecommerce_config.dynamic.php");
	$files[] = array($basepath . $kw_actions . "product_maintenance.dynamic.php", $basepath . $mngrpath . "actions/product_maintenance.dynamic.php");
	$files[] = array($basepath . $kw_actions . "list_maintenance.dynamic.php", $basepath . $mngrpath . "actions/list_maintenance.dynamic.php");
	$files[] = array($basepath . $kw_actions . "tax_maintenance.dynamic.php", $basepath . $mngrpath . "actions/tax_maintenance.dynamic.php");
	$files[] = array($basepath . $kw_actions . "discount_maintenance.dynamic.php", $basepath . $mngrpath . "actions/discount_maintenance.dynamic.php");
	$files[] = array($basepath . $kw_actions . "options_maintenance.dynamic.php", $basepath . $mngrpath . "actions/options_maintenance.dynamic.php");
	$files[] = array($basepath . $kw_actions . "order_maintenance.dynamic.php", $basepath . $mngrpath . "actions/order_maintenance.dynamic.php");
	$files[] = array($basepath . $kw_actions . "customer_maintenance.dynamic.php", $basepath . $mngrpath . "actions/customer_maintenance.dynamic.php");
	$files[] = array($basepath . $kw_actions . "lang/english.inc.php", $basepath . $mngrpath . "includes/lang/english.inc.php");
	$files[] = array($basepath . $kw_actions . "save_kiwee_settings.processor.php", $basepath . $mngrpath . "processors/save_kiwee_settings.processor.php");
	
	installfiles('Administration interface', $files);
	unset($files);
	

	// Copy admin page includes
	$files = array();
	$files[] = array($basepath . $kw_actions . "includes/dynform.js", $basepath . $mngrpath . "actions/includes/dynform.js");	
	$files[] = array($basepath . $kw_actions . "includes/interface.js", $basepath . $mngrpath . "actions/includes/interface.js");
	$files[] = array($basepath . $kw_actions . "includes/md5.js", $basepath . $mngrpath . "actions/includes/md5.js");
		
	installfiles('Administration includes', $files);
	unset($files);

	// Overwrite the existing modx manager index file
	// TODO: Update to insert into existing files instead of overwriting. This is dangerous!
	$files = array();
	$files[] = array($basepath . $kw_manger . 'index.php', $basepath . $mngrpath . 'index.php');
	$files[] = array($basepath . $kw_manager . 'frames/menu.php', $basepath . $mngrpath . 'frames/menu.php');
	
	installfiles('Administration pages', $files);

	////////////////////////////////////////////////////
	// Connect to handle ECommerce Module Installation
	$database_connection_charset = 'utf8';
	$setupPath = realpath(dirname(__FILE__));
	include "$setupPath/install_files/sqlParser.class.php";

	$sqlParser = new SqlParser($database_server, $database_user, $database_password, str_replace("'", "", $dbase), $table_prefix, $adminname, $adminemail, $adminpass, $database_connection_charset);
	$sqlParser->modx = ($installMode < 1) ? "new" : "upd";
	$sqlParser->connect();

	///////////////////////////////////////////
	// Create Category for all ECommerce Stuff
	
	$qry = "REPLACE INTO " . $table_prefix . "categories (id, category) VALUES (4000, \"ITema Ecommerce\")";
	$result = mysql_query($qry, $sqlParser->conn);

	$qry = "REPLACE INTO " . $table_prefix . "categories (id, category) VALUES (4001, \"ITema Content Area Templates\")";
	$result = mysql_query($qry, $sqlParser->conn);

	///////////////////////////////////////
	//Files to install

	$dbfolder = 'kiwee/db/';
	$sql[0] = 'kiwee_schema.sql';
	$sql[1] = 'site_htmlsnippets.sql';
	$sql[2] = 'kiwee_config.sql';
	$sql[3] = 'kiwee_config_values.sql';
	///////////////////////////////////////
	
	for ($i = 0; $i < sizeof($sql); $i++){
		$sqlParser->process($dbfolder . $sql[$i]);	
	}

	echo '<tr><td>Database schema and entries:&nbsp;&nbsp;&nbsp;</td>';
    if ($sqlParser->installFailed == true) {
		echo '<td><font color="#990000">ERROR:</font><br />';
		for ($i = 0; $i < count($sqlParser->mysqlErrors); $i++) {
			echo $sqlParser->mysqlErrors[$i]["error"] . '<br />';
			echo $sqlParser->mysqlErrors[$i]["sql"] . '<br />';
		}
		echo '</td></tr>';
		exit;
	} else {
		echo '<td><font color="#009900">OK</font> (' . $i . ' installed)</td></tr>';
	}

	//////////////////////////////////////////
	//Install Snippets
	$snipdir = "kiwee/resources/snippets/";
	include_once($snipdir . 'installconfig.inc');	

	for ($i = 0; $i < sizeof($snip); $i++){
		$name = mysql_escape_string($snip[$i]['name']);	
		$desc = mysql_escape_string($snip[$i]['desc']);	
		$fil  = file_get_contents($snipdir . $snip[$i]['filename']);
		$filecontent = mysql_escape_string($fil);
		$qry  = "REPLACE INTO $dbase.`" . $table_prefix . "site_snippets` (id, name, description, editor_type, category, cache_type, snippet, locked, properties, moduleguid) VALUES (";
		$qry .= $snip[$i]['pageid'] . ", \"" . $snip[$i]['name'] . "\", \"" . $snip[$i]['desc'] . "\", 0, $ctgy, 0, \"";
		$qry .= $filecontent;																																													   
		$qry .= "\", 0, \"\", \"  \")";
		$result = mysql_query($qry, $sqlParser->conn);
	}
	echo '<tr><td>Snippets:&nbsp;&nbsp;&nbsp;</td>';
	echo '<td><font color="#009900">OK</font> (' . $i . ' installed)</td></tr>';

	//////////////////////////////////////////////////////////////
	// Install Site Templates
	$tmpldir = "kiwee/resources/templates/";
	include_once($tmpldir . 'installconfig.inc');
		
     for ($i = 0; $i < sizeof($tmpl); $i++){
		$pageid = $tmpl[$i]['pageid'];
        $templatename = mysql_escape_string($tmpl[$i]['templatename']);
        $description = mysql_escape_string($tmpl[$i]['description']);
		$fil  = file_get_contents($tmpldir . $tmpl[$i]['filename']);
		$filecontent = mysql_escape_string($fil);
		$qry  = "REPLACE INTO $dbase.`" . $table_prefix. "site_templates` (id, templatename, description, editor_type, category, icon, template_type, content, locked) VALUES (";
		$qry .= $pageid . ", \"" . $templatename . "\", \"" . $description . "\", 0, \"" . $ctgy . "\", \"\", 0, \"";
		$qry .= $filecontent;
		$qry .= "\", 0)";
		$result = mysql_query($qry, $sqlParser->conn) or die ("MYSQL ERROR: " . mysql_error());
	 }
	echo '<tr><td>Site templates:&nbsp;&nbsp;&nbsp;</td>';
	echo '<td><font color="#009900">OK</font> (' . $i . ' installed)</td></tr>';

	$files = array();
	$files[] = array($basepath . $kw_templates ."css/global.css", $basepath . "assets/templates/kiwee/css/global.css");
	$files[] = array($basepath . $kw_templates . "images/bg.gif", $basepath . "assets/templates/kiwee/images/bg.gif");	
	
	installfiles('Administration interface', $files);
	unset($files);

    ////////////////////////////////////////////////////////////
	// Install Site Content Pages
	$contdir = "kiwee/resources/content/";
	include_once($contdir. 'installconfig.inc');
	
    for ($i = 0; $i < sizeof($cont); $i++){
		$pageid = $cont[$i]['pageid'];
		$title = mysql_escape_string($cont[$i]['pagetitle']);
		$longtitle = mysql_escape_string($cont[$i]['longtitle']);
		$descr = mysql_escape_string($cont[$i]['description']);
		$isfolder = $cont[$i]['isfolder'];
		$template = $cont[$i]['template'];
		$parent = $cont[$i]['parent'];

		$fil  = file_get_contents($contdir . $cont[$i]['filename']);
		$filecontent = mysql_escape_string($fil);
		
		$qry  = "REPLACE INTO $dbase.`" . $table_prefix. "site_content` (id, type, contentType, pagetitle, longtitle, description, alias, ";
		$qry .= "link_attributes, published, pub_date, unpub_date, parent, isfolder, introtext, content, richtext, template, menuindex, ";
		$qry .= "searchable, cacheable, createdby, createdon, editedby, editedon, deleted, deletedon, deletedby, publishedon, publishedby, ";
		$qry .= "menutitle, donthit, haskeywords, hasmetatags, privateweb, privatemgr, content_dispo, hidemenu) VALUES (";
		$qry .= $pageid . ", \"document\", \"text/html\", \"" . $title . "\", \"" . $longtitle . "\", \"\", \"\", \"\", 1, 0, 0, " . $parent . ", " . $isfolder. ", \"\", \"";
		$qry .= $filecontent;
		$qry .= "\", 1, " . $template . ", 0, 0, 0, 1, " . time() . ", 1, 1173881912, 0, 0, 0, 1173881526, 1, \"\", 0, 0, 0, 0, 0, 0, 1)";
		
		$result = mysql_query($qry, $sqlParser->conn);
	}
	echo '<tr><td>Site content:&nbsp;&nbsp;&nbsp;</td>';
	echo '<td><font color="#009900">OK</font> (' . $i . ' installed)</td></tr>';

	//////////////////////////////////////////////////////////////
	// Install Template Variables

	$tmplvardir = "kiwee/resources/tmplvars/";
	include_once($tmplvardir . 'installconfig.inc');
	for ($i = 0; $i < sizeof($tmplvar); $i++){
         $pageid = $tmplvar[$i]['pageid'];
         $name = mysql_escape_string($tmplvar[$i]['name']);
         $caption = mysql_escape_string($tmplvar[$i]['caption']);
		 $editortype = $tmplvar[$i]['editortype'];
 
         $qry  = "REPLACE INTO $dbase.`" . $table_prefix. "site_tmplvars` (id, type, name, caption, description, editor_type, category, locked, elements, ";
		 $qry .= "rank, display, display_params, default_text) VALUES (";
		 $qry .= $pageid . ", \"" . $editortype . "\", \"" . $name . "\", \"" . $caption . "\", \"\", 0, " . $ctgy . ", 0, \"\", 0, \"\", \"\", \"\")"; 
         $result = mysql_query($qry, $sqlParser->conn);
	}
	echo '<tr><td>Template variables:&nbsp;&nbsp;&nbsp;</td>';
	echo '<td><font color="#009900">OK</font> (' . $i . ' installed)</td></tr>';

	//////////////////////////////////////////////////////////////
	// Install template variable templates
    $tmplvartpldir = "kiwee/resources/tmplvartpl/";
    include_once($tmplvartpldir . 'installconfig.inc');

     for ($i = 0; $i < sizeof($tmplvartpl); $i++){
         $templatevariable = $tmplvartpl[$i]['tmplvarid'];
         $templateid = $tmplvartpl[$i]['templateid'];
	 		 
         $qry  = "REPLACE INTO $dbase.`" . $table_prefix. "site_tmplvar_templates` (tmplvarid, templateid, rank) VALUES (";
         $qry .= $templatevariable . ", " . $templateid . ",0)";
         $result = mysql_query($qry, $sqlParser->conn);
      }
	echo '<tr><td>Template variable templates:&nbsp;&nbsp;&nbsp;</td>';
	echo '<td><font color="#009900">OK</font> (' . $i . ' installed)</td></tr>';

    ////////////////////////////////////////////////////////////
	// Install Template Variable Content Values
	$tmplvarcontdir = "kiwee/resources/tmplvarcontent/";
	include_once($tmplvarcontdir . 'installconfig.inc');

    for ($i = 0; $i < sizeof($tmplvarcontent); $i++){		
		$tmplvarid = $tmplvarcontent[$i]['tmplvarid'];
		$contentid = $tmplvarcontent[$i]['contentid'];
		$pageid = $tmplvarcontent[$i]['pageid'];
		
		$fil  = file_get_contents($tmplvarcontdir . $tmplvarcontent[$i]['filename']);
		$filecontent = mysql_escape_string($fil);
		
		$qry  = "REPLACE INTO $dbase.`" . $table_prefix. "site_tmplvar_contentvalues` (id, tmplvarid, contentid, value) VALUES (";
		$qry .= $pageid . ", " . $tmplvarid . ", " . $contentid . ", \"";
		$qry .= $filecontent;
		$qry .= "\")";
		$result = mysql_query($qry, $sqlParser->conn);
	}
	echo '<tr><td>Template variable content:&nbsp;&nbsp;&nbsp;</td>';
	echo '<td><font color="#009900">OK</font> (' . $i . ' installed)</td></tr>';
?>
</table>